package com.bw.network_sdk;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public abstract class BaseHttpRetrofitImpl implements HttpRetrofit{

    //构建okhttpBuilder方法
    protected OkHttpClient.Builder createokHttpBuilder(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(readtime(), TimeUnit.SECONDS);
        builder.connectTimeout(connectime(),TimeUnit.SECONDS);
        builder.writeTimeout(writetime(),TimeUnit.SECONDS);
        return builder;
    }
    protected abstract long readtime();
    protected abstract long connectime();
    protected abstract long writetime();

    @Override
    public Retrofit createRetrofit(OkHttpClient client) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.client(client)
                .baseUrl(ApiConst.BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());
        return builder.build();
    }
}
